import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/components/common/Layout'
import Notfound from '@/components/common/Notfound'
import HelloWorld from '@/components/HelloWorld'
import Product from '@/components/pages/Product'
import User from '@/components/pages/User'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      meta: { title: '你好，世界' }, // 标题保存在meta.title中，由路由beforeEach实现title切换
      component: HelloWorld
    },
    {
      path: '*',
      name: '404',
      component: Notfound
    },
    {
      path: '/admin',
      component: Layout,
      meta: {
        requireAuth: true
      },
      children: [
        { path: '/', component: Product, name: 'index', meta: { title: '产品管理' } },
        { path: 'user', component: User, name: 'user', meta: { title: '用户管理' } }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  /* let token = localStorage.getItem('access_token')
  let nowTime = new Date()
  if (to.matched.some(r => r.meta.requireAuth)) {
    if (token && (nowTime.getTime() < localStorage.getItem('expires_time') * 1000)) {
      next()
    } else {
      localStorage.clear()
      next({path: '/'})
    }
  } else {
    next()
  } */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
